package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/7.
 */

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzPhase;
import com.xianzhiOA.bean.XzWork;
import com.xianzhiOA.bean.XzWorking;
import com.xianzhiOA.dao.XzPersonMapper;
import com.xianzhiOA.dao.XzPhaseMapper;
import com.xianzhiOA.dao.XzWorkingMapper;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.service.XzWorkingService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作量业务层
 *
 * @author 甘汝雷
 * @create 2017-05-07 15:39
 **/
@Service("xzWorkingService")
public class XzWorkingServiceImpl implements XzWorkingService {
    @Autowired
    private XzWorkingMapper workingMapper;
    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzPhaseMapper phaseMapper;
    //添加
    @Override
    public int insert(XzWorking working) {
        working = myWorking(working);
        workingMapper.insert(working);
        for (XzPhase p:working.getPhaseList()) {
            p.setPhaseWorking(working.getWorkingId());
            phaseMapper.insert(p);
        }
        return 0;
    }
    //完善工作量分布信息
    public XzWorking myWorking(XzWorking working){
        XzWork work = new XzWork();
        work.setEmpId(working.getEmpId());
        work.setWorkWeekTime(working.getWorkingTime());
        List<XzWork> works = workService.selAll(work);
        int i = 0,j = 0;//一周的计划时间、一周的实际时间。
        int p1=0,p2=0,a1=0,a2=0;//计划比例、计划工作量、实际比例、实际工作量。
        for (XzWork w:works) {
            i +=(int)w.getWorkPlan();
            j += (int)w.getWorkActual();
        }
        //计算并保存比例、工作量及统计
        for (XzPhase p:working.getPhaseList()){
            if (i!=0)p.setPhasePlansc(p.getPhasePlan()*100/i);
            if (j!=0)p.setPhaseActualsc(p.getPhaseActual()*100/j);
            p1 += p.getPhasePlansc();
            p2 += p.getPhasePlan();
            a1 += p.getPhaseActualsc();
            a2 += p.getPhaseActual();
        }

        //计算并保存总比例和工作量信息
        working.setWorkingPlansc(p1);
        working.setWorkingActualsc(a1);
        working.setWorkingPlan(p2);
        working.setWorkingActual(a2);
        return working;
    }

    //删除
    @Override
    public int delete(Long workingId) {
        return workingMapper.delete(workingId);
    }

    //查询方法重载
    @Override
    public List<XzWorking> selAll(Map map) {
        return workingMapper.selAll(map);
    }

    //伪删除
    @Override
    public int isdelete(Long workingId) {
        Map map = new HashMap();
        map.put("workingId",workingId);
        map.put("isdelete",1);
        return workingMapper.update(map);
    }

    //修改
    @Override
    public int update(XzWorking working) {
        working = myWorking(working);
        workingMapper.update(myMap(working));
        for (XzPhase p:working.getPhaseList()) {
            phaseMapper.update(p);
        }
        return 0;
    }

    //把working转换成map
    public Map myMap(XzWorking working){
        Map map = new HashMap();
        map.put("workingId",working.getWorkingId());
        map.put("workingProject",working.getWorkingProject());
        map.put("workingPlansc",working.getWorkingPlansc());
        map.put("workingPlan",working.getWorkingPlan());
        map.put("workingActualsc",working.getWorkingActualsc());
        map.put("workingActual",working.getWorkingActual());
        map.put("workingWeekCodes",working.getWorkingWeekCodes());
        map.put("workingWorkTime",working.getWorkingWorkTime());
        map.put("createTime",working.getCreateTime());
        map.put("workingTime",working.getWorkingTime());
        map.put("empId",working.getEmpId());
        map.put("empName",working.getEmpName());
        if (working.getProject()!=null)map.put("projectName",working.getProject().getProjectName());
        return map;
    }

    //分页查询
    @Override
    public PageBean<XzWorking> selAll(XzWorking working, int page, int rows, Date begin1, Date end1, Date begin2, Date end2) {
        PageHelper.startPage(page,rows);
        Map map = myMap(working);
        if (working.getEmpName()!=null)map.put("empName",working.getEmpName());
        if (begin1!=null) map.put("begin1",begin1);
        if (end1!=null) map.put("end1",end1);
        if (begin2!=null) map.put("begin2",begin2);
        if (end2!=null) map.put("end2",end2);
        List<XzWorking> workings = workingMapper.selAll(map);
        PageBean<XzWorking> pageBean = new PageBean<>(workings);
        return pageBean;
    }

    @Override
    public XzWorking selById(Long workingId) {
        return workingMapper.selById(workingId);
    }
}
